package com.sright.bcard.mini.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.sright.bcard.common.service.share.enums.SourceTypeEnum;
import com.sright.bcard.common.service.share.okhttp.domain.AuthDomain;
import com.sright.bcard.common.service.share.okhttp.domain.AuthFullParam;
import com.sright.bcard.common.share.request.CreateQrCodeParam;
import com.sright.bcard.common.share.request.FileTranscodeParam;
import com.sright.bcard.common.share.request.VideoCheckCallbackParam;
import com.sright.bcard.common.share.service.AuthUploadService;
import com.sright.bcard.common.share.service.ContentCheckService;
import com.sright.bcard.common.share.service.TranscodeService;
import com.sright.bcard.common.share.service.WxappService;
import com.sright.bcard.common.service.share.dto.content.CardSettingDTO;
import com.sright.bcard.content.share.request.FavoriteRequest;
import com.sright.bcard.content.share.request.FeedBackParamRequest;
import com.sright.bcard.content.share.request.ShareImageParamRequest;
import com.sright.bcard.content.share.service.CardSettingService;
import com.sright.bcard.content.share.service.FavoriteCardService;
import com.sright.bcard.content.share.service.FavoriteService;
import com.sright.bcard.content.share.service.FeedbackService;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 共用接口controller
 */
@RestController
public class CommonController {

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private CardSettingService cardSettingService;

    @Autowired
    private AuthUploadService authUploadService;

    @Autowired
    private FavoriteService favoriteService;

    @Autowired
    private FavoriteCardService favoriteCardService;

    @Autowired
    private WxappService wxappService;

    @Autowired
    private TranscodeService transcodeService;

    @Autowired
    private ContentCheckService contentCheckService;

    /**
     * 提交用户反馈
     * @param feedBackParamRequest
     * @param tokenParam
     * @return
     */
    @PostMapping(WebUrl.Feedback)
    public SrightResponse feedback(@RequestBody FeedBackParamRequest feedBackParamRequest,@RequestAttribute TokenParam tokenParam) {
        buildFeedbackContext(feedBackParamRequest,tokenParam);
        return SrightResponse.defaultSuccess(feedbackService.addFeedback(feedBackParamRequest));
    }

    /**
     * 编辑用户设置
     * @param cardSettingDTO
     * @param tokenParam
     * @return
     */
    @PostMapping(WebUrl.UserSetting)
    public SrightResponse userSetting(@RequestBody CardSettingDTO cardSettingDTO, @RequestAttribute TokenParam tokenParam) {
        buildUserSettingContext(cardSettingDTO,tokenParam);
        return cardSettingService.userSetting(cardSettingDTO);
    }

    /**
     * 设置用户分享图
     * @param shareImageParamRequest
     * @param tokenParam
     * @return
     */
    @PutMapping(WebUrl.UserSetImg)
    public SrightResponse setimg(@RequestBody ShareImageParamRequest shareImageParamRequest, @RequestAttribute TokenParam tokenParam) {
        buildShareSettingContext(shareImageParamRequest,tokenParam);
        return cardSettingService.setimg(shareImageParamRequest);
    }

    /**
     * 获取用户设置信息
     * @return
     */
    @GetMapping(WebUrl.GetUserSetting)
    public SrightResponse getSetting(@RequestParam("userId") Long userId) {
        return cardSettingService.getSetting(userId);
    }

    /**
     * 设置手机号
     * @param mobile
     * @param tokenParam
     * @return
     */
    @GetMapping(WebUrl.SetMobile)
    public SrightResponse setMobile(@RequestParam("mobile") String mobile, @RequestAttribute TokenParam tokenParam) {
        return cardSettingService.setMobile(mobile,tokenParam.getMemberId());
    }

    /**
     * 个人名片素材上传
     * @param authDomain
     * @return
     */
    @PostMapping(WebUrl.UploadFile)
    public SrightResponse upload(@RequestBody AuthDomain authDomain) {
        AuthFullParam authFullParam = new AuthFullParam();
        authFullParam.setAuthDomain(authDomain);
        return authUploadService.auth(authFullParam);
    }

    /**
     * 添加收藏
     * @return
     */
    @PostMapping(WebUrl.Favorite)
    public SrightResponse addFavorite(@RequestBody FavoriteRequest favoriteRequest, @RequestAttribute TokenParam tokenParam) {
        buildFavoriteCardContext(favoriteRequest,tokenParam);
        return favoriteService.addFavorite(favoriteRequest);
    }

    /**
     * 名片夹
     * @param keywords 搜索名片姓名
     * @return
     */
    @GetMapping(WebUrl.Favorite)
    public SrightResponse cardHolder(Integer page,String keywords,@RequestAttribute TokenParam tokenParam) {
        return favoriteCardService.cardHolder(page,keywords,tokenParam.getMemberId());
    }

    /**
     * 生成二维码
     * @param param
     * @return
     * @throws Exception
     */
    @GetMapping(WebUrl.QRCode)
    public SrightResponse qrCode(CreateQrCodeParam param) throws Exception {
        String path = "pages/card/main?t=3&cardId=" + param.getCardId() + "&userId=" + param.getUserId() + "&lang=zh&u=" + param.getUserType();
        return wxappService.createQrCode(path);
    }

    /**
     * 视频转码回调处理
     * @param mtsRequest
     * @return
     */
    @PostMapping(WebUrl.Callback)
    public SrightResponse transcodeCallback(@RequestBody String mtsRequest) {
        return transcodeService.callback(mtsRequest);
    }

    @GetMapping(WebUrl.TextCheck)
    public SrightResponse textCheck(@RequestParam String content, TokenParam tokenParam) {
        List<String> list = new LinkedList<>();
        list.add(content);
        return SrightResponse.defaultSuccess(contentCheckService.textCheck(list,tokenParam.getMemberId(), SourceTypeEnum.APPLET.getValue()));
    }

    private FeedBackParamRequest buildFeedbackContext(FeedBackParamRequest feedBackParamRequest, TokenParam tokenParam) {
        feedBackParamRequest.setMemberId(tokenParam.getMemberId());
        feedBackParamRequest.setEnterpriseId(tokenParam.getEnterpriseId());
        return feedBackParamRequest;
    }

    private CardSettingDTO buildUserSettingContext(CardSettingDTO cardSettingDTO, TokenParam tokenParam) {
        cardSettingDTO.setMemberId(tokenParam.getMemberId());
        cardSettingDTO.setEnterpriseId(tokenParam.getEnterpriseId());
        return cardSettingDTO;
    }

    private ShareImageParamRequest buildShareSettingContext(ShareImageParamRequest shareImageParamRequest, TokenParam tokenParam) {
        shareImageParamRequest.setMemberId(tokenParam.getMemberId());
        shareImageParamRequest.setEnterpriseId(tokenParam.getEnterpriseId());
        return shareImageParamRequest;
    }

    private FavoriteRequest buildFavoriteCardContext(FavoriteRequest request, TokenParam tokenParam) {
        request.setMemberId(tokenParam.getMemberId());
        request.setEnterpriseId(tokenParam.getEnterpriseId());
        return request;
    }
}
